/**
 * @Title: BlogIndexController.java
 * @Package com.zll.ccp.controller.blog
 * @author zhanglinlu
 * @date 2019年5月9日 下午1:31:41
 */
package com.zll.ccp.controller.blog;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zll.ccp.annotation.RequireOwn;
import com.zll.ccp.model.pojo.ResourceType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.zll.ccp.model.blog.BlogType;
import com.zll.ccp.service.blog.IBlogService;

/**
 * @ClassName: BlogIndexController
 * @Description: 博客主页
 * @author zhanglinlu
 * @date 2019年5月9日 下午1:31:41
 * 
 */
@Controller
public class BlogIndexController {
    @Autowired
    private IBlogService blogService;

    /**
     * @Title: blogindex
     * @Description: 进入博客主页
     * @return
     */
    @RequestMapping("/blog")
    public String blogindex() {
        return "blog/blog";
    }

    /**
     * @Title: initBlogType
     * @Description: 主页获取博客类型初始化数据
     * @return
     */
    @GetMapping("/initblogtype")
    @ResponseBody
    public Object initBlogType() {
        List<BlogType> blogTypeList = blogService.getBlogTypeList();
        BlogType blogType = new BlogType();
        blogType.setId("0");
        blogType.setTypeName("全部");
        blogTypeList.add(0, blogType);
        return blogTypeList;
    }

    /**
     * @Title: getBlogListOfPage
     * @Description: 分页查询博客内容
     * @param param 筛选条件
     * @param nowPage 当前页
     * @return 博客分页数据
     */
    @GetMapping("/blogsofpage")
    @ResponseBody
    public Object getBlogListOfPage(String typeId, String userId, String title, int nowPage, int pagesize) {
        JSONObject result = new JSONObject();
        Map<String, Object> param = new HashMap<>();
        if (!StringUtils.equals(typeId, "0")) {
            param.put("typeId", typeId);
        }
        if (!StringUtils.isBlank(title)) {
            param.put("title", title);
        }
        if (!StringUtils.isBlank(userId)) {
            List<String> userIds = blogService.getUserIdsByName(userId);
            param.put("userIds", userIds);
        }
        PageInfo<Map<String, Object>> pageInfo = blogService.getBlogListByFilter(param, nowPage, pagesize);
        List<Map<String, Object>> blogList = pageInfo.getList();
        result.put("blogs", blogList);
        result.put("pages", pageInfo.getPages());
        result.put("blogCount", pageInfo.getTotal());
        return result;
    }

    /**
     * @Title: deleteBlog
     * @Description: 删除博客
     * @param id 博客id
     * @return
     */
    @RequireOwn(resourceType = ResourceType.blog,paramName = "id")
    @GetMapping("/deleteblog")
    @ResponseBody
    public Object deleteBlog(String id) {
        blogService.deleteBlogByid(id);
        return null;
    }
}
